package com.woniuxy.micromallserver.categorymag.controller;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.common.bean.PageDto;
import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.micromallserver.bean.Category;
import com.woniuxy.micromallserver.categorymag.service.CategoryService;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Chan
 * @Date 2021/1/21 14:30
 */
@CrossOrigin
@RequestMapping("/category")
@RestController
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private ValueOperations valueOperations;

    /**分页查询*/
    @GetMapping(value = "/getByPage")
    public ResultMsg getByPage(Integer pageNumber, Integer pageSize, Category category){

        ResultMsg resultMsg = new ResultMsg();

        //无查询条件时查询全部
        if(category == null){
            PageDto  data = (PageDto) valueOperations.get("pageDto" + pageNumber + pageSize);
            System.out.println("PageDto data:" + data);
            if(data == null){
                PageDto pageDto = categoryService.getCategoriesByPage(pageNumber, pageSize, null);
                if (pageDto == null) {
                    return resultMsg.setCode(8899).setMessage("失败");
                }
                valueOperations.set("pageDto" + pageNumber + pageSize, pageDto);

                return resultMsg.setCode(200).setMessage("成功").setData(pageDto);
            }else {
                System.out.println("redis:");
                System.out.println("data:" + data);
                return resultMsg.setCode(200).setMessage("成功").setData(data);
            }
        }

        //根据查询条件查询
        PageDto data = (PageDto) valueOperations.get("pageDto" + pageNumber + pageSize);
        if (data == null){
            PageDto pageDto = categoryService.getCategoriesByPage(pageNumber, pageSize, category);
            if (pageDto == null) {
                return resultMsg.setCode(8899).setMessage("失败");
            }
            valueOperations.set("pageDto" + pageNumber + pageSize + category.toString(), pageDto);
            return resultMsg.setCode(200).setMessage("成功").setData(pageDto);
        }else {
            System.out.println("redis:");
            return resultMsg.setCode(200).setMessage("成功").setData(data);
        }


    }

    /**根据id查询*/
    @GetMapping(value = "/getCategoryById/{categoryId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCategoryById(@PathVariable("categoryId") int categoryId){
        Category categoryById = categoryService.getCategoryById(categoryId);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(categoryById);

        return JSONObject.toJSONString(resultMsg);
    }

    /**获取所有品类*/
    @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getAll(){
        List<Category> allCategories = categoryService.getAllCategories();
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(allCategories);

        return JSONObject.toJSONString(resultMsg);
    }

    /**条件查询*/
    @PostMapping(value = "/categoriesByConditions", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCategoryByConditions(@RequestBody Category category){
        List<Category> categoriesByConditions = categoryService.getCategoriesByMultiConditions(category);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(categoriesByConditions);

        return JSONObject.toJSONString(resultMsg);
    }

    /**新增品类*/
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addCategory(@RequestBody Category category){
        if (categoryService.addCategory(category) == 1){
            ResultMsg resultMsg = new ResultMsg(true);
            return JSONObject.toJSONString(resultMsg);
        }
        return JSONObject.toJSONString(new ResultMsg(false));
    }

    /**更改品类状态*/
    @PostMapping(value = "/{categoryId}/{categoryStatus}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteCategory(@PathVariable("categoryId") Integer categoryId, @PathVariable("categoryStatus") Integer categoryStatus){
        categoryService.changeCategoryStatus(categoryId, categoryStatus);
        return JSONObject.toJSONString(new ResultMsg(true));
    }

	/**编辑分类*/
	@PostMapping(value = "/editCategory", produces = MediaType.APPLICATION_JSON_VALUE)
	public String editCategory(@RequestBody Category category){
        int i = categoryService.updateCategory(category.getCategoryId(), category.getCategoryName(), category.getCategoryOrder(),
                category.getSubcategory(), category.getCategoryDescription());
        if(i == 1){
            ResultMsg resultMsg = new ResultMsg(true);
            return JSONObject.toJSONString(resultMsg);
        }else {
            ResultMsg resultMsg = new ResultMsg(false);
            return JSONObject.toJSONString(resultMsg);
        }
    }

}
